//
//  WFActivityViewController.m
//  meizhuang
//
//  Created by Ricky on 16/6/14.
//  Copyright © 2016年 netease. All rights reserved.
//

#import <Masonry/Masonry.h>

#import "WFActivityViewController.h"

@interface WFActivityViewController () <UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning>
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *addButton;
@end

@implementation WFActivityViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.contentHeight = 300.f;
        self.transitioningDelegate = self;
        self.modalPresentationStyle = UIModalPresentationCustom;
    }
    return self;
}

- (void)loadView
{
    self.view = [[UIControl alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view.backgroundColor = [UIColor clearColor];
    [(UIControl *)self.view addTarget:self
                               action:@selector(onDismiss:)
                     forControlEvents:UIControlEventTouchDown];
    
    self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.contentHeight)];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.bottom = self.view.height;
    self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [self.view addSubview:self.contentView];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNeedsStatusBarAppearanceUpdate];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (self.isBeingPresented) {
        self.backgroundView = [[UIView alloc] init];
        self.backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:.6];
        self.backgroundView.userInteractionEnabled = NO;
        self.backgroundView.alpha = 0;
    }
}

- (BOOL)shouldAutorotate
{
    return NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    if (self.presentingViewController) {
        self.view.frame = self.presentingViewController.view.frame;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (void)setContentHeight:(CGFloat)contentHeight
{
    if (_contentHeight != contentHeight) {
        _contentHeight = contentHeight;
        if (self.isViewLoaded) {
            self.contentView.height = contentHeight;
        }
    }
}

- (void)setBackgroundView:(UIView *)backgroundView
{
    if (_backgroundView != backgroundView) {
        [_backgroundView removeFromSuperview];
        
        _backgroundView = backgroundView;
        
        _backgroundView.frame = self.view.bounds;
        _backgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        [self.view insertSubview:_backgroundView atIndex:0];
    }
}

- (void)onDismiss:(id)sender
{
    [self dismissViewControllerAnimated:YES
                             completion:NULL];
}

#pragma mark - UIViewController Transitioning

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
                                                                   presentingController:(UIViewController *)presenting
                                                                       sourceController:(UIViewController *)source
{
    return self;
}

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    return self;
}

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
    return UINavigationControllerHideShowBarDuration;
}

// This method can only  be a nop if the transition is interactive and not a percentDriven interactive transition.
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
    WFActivityViewController *from = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    WFActivityViewController *to = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    // presenting
    if ([to isKindOfClass:[WFActivityViewController class]]) {
        UIView *containerView = [transitionContext containerView];
        to.view.frame = containerView.bounds;
        to.contentView.top = to.view.height;
        [containerView addSubview:to.view];
        
        [UIView animateWithDuration:[self transitionDuration:transitionContext]
                              delay:0
                            options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             to.backgroundView.alpha = 1;
                             to.contentView.bottom = containerView.height - containerView.top;
                         }
                         completion:^(BOOL finished) {
                             [transitionContext completeTransition:!transitionContext.transitionWasCancelled];
                         }];
    }
    // dismissing
    else if ([from isKindOfClass:[WFActivityViewController class]]) {
        [UIView animateWithDuration:[self transitionDuration:transitionContext]
                              delay:0
                            options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             from.contentView.top = from.view.height;
                             from.backgroundView.alpha = 0;
                         }
                         completion:^(BOOL finished) {
                             [transitionContext completeTransition:!transitionContext.transitionWasCancelled];
                         }];
    }
}


@end
